package org.layuiframework.generator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.engine.VelocityTemplateEngine;
import org.layuiframework.generator.config.LayuiConfigBuilder;
import org.layuiframework.generator.engine.LayuiTableInfoLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class LayuiAutoGenerator extends AutoGenerator {

    private static final Logger logger = LoggerFactory.getLogger(LayuiAutoGenerator.class);

    @Override
    public void execute() {
        logger.debug("==========================准备生成文件...==========================");
        if (null == this.config) {
            this.config = new LayuiConfigBuilder(this.getPackageInfo(), this.getDataSource(), this.getStrategy(), this.getTemplate(), this.getGlobalConfig());
            if (null != this.injectionConfig) {
                this.injectionConfig.setConfig(this.config);
            }
        }
        if (null == this.getTemplateEngine()) {
            this.setTemplateEngine(new VelocityTemplateEngine());
        }
        // 读取json文件
        String filePath = System.getProperty("user.dir") + File.separator + "web" + File.separator + "table.json";
        LayuiTableInfoLoader.init(filePath);
        this.getTemplateEngine().init(this.pretreatmentConfigBuilder(this.config)).mkdirs().batchOutput().open();
//        this.getTemplateEngine().init(this.pretreatmentConfigBuilder(this.config)).batchOutput();
        // 保存json文件
        LayuiTableInfoLoader.destroy(filePath);
        logger.debug("==========================文件生成完成！！！==========================");
    }
}
